using System;
using System.Collections.Generic;
using Cleverscape.UTorrentClient.WebClient;
using System.Linq;
using SZipHandler.Entities;

namespace SZipHandler
{
    public class uTorrentConnector
    {
        private string _server;
        private int _port;
        private string _user;
        private string _password;
        private readonly string _downloadFolderPropKey;

        private readonly string hash;

        public uTorrentConnector(string server, int port, string user, string password, string downloadFolderPropKey)
        {
            _server = server;
            _port = port;
            _user = user;
            _password = password;
            _downloadFolderPropKey = downloadFolderPropKey;

            hash = Guid.NewGuid().ToString();
        }


        public TorrentFilesHelper[] GetTorrentFiles()
        {
            //TestRun
            //var testList = new List<TorrentFilesHelper>();
            //testList.Add(new TorrentFilesHelper
            //                 {
            //                     Files = new string[] { @"v.zip.001" },
            //                     Folder = @"D:\t",
            //                     Hash = hash,
            //                     Name = "Test"
            //                 });

            //return testList.ToArray();


            var helperList = new List<TorrentFilesHelper>();

            using(var client = new UTorrentWebClient(_server + ":" + _port.ToString(), _user, _password, 10))
            {
                var torrents = client.Torrents;
                
                foreach (var torrent in torrents.Where(x => x.SimpleStatus == TorrentSimpleStatus.Seeding
                                                         || x.SimpleStatus == TorrentSimpleStatus.SeedingError
                                                         || x.SimpleStatus == TorrentSimpleStatus.SeedingQueued
                                                         || x.SimpleStatus == TorrentSimpleStatus.InactiveCompleted))
                {
                    var helper = new TorrentFilesHelper
                                     {
                                         Files = torrent.Files.Select(x => x.Name).ToArray(),
                                         Folder = getDownloadFolder(client),
                                         Hash = torrent.Hash,
                                         Name = torrent.Name
                                     };
                    helperList.Add(helper);
                }
            }

            return helperList.ToArray();
        }

        private string getDownloadFolder(UTorrentWebClient client)
        {
            var settings = client.Settings;

            var folder = settings.FirstOrDefault(x => x.Name == _downloadFolderPropKey);

            if (folder == null)
                throw new ArgumentException("DownloadFolderPropKey not found!");

            switch (folder.Type)
            {
                case SettingType.String:
                    var f = (SettingString) folder;
                    return f.Value;
                    break;
                default:
                    throw new ArgumentException("Wrong DownloadFolderPropKey!");
            }
        }
    }
}